<template>
  <a-modal
    title="下载"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    :footer="false"
  >
    <!-- `Field ${i}` -->
    <a-spin :spinning="formLoading">
      <div id="components-form-demo-advanced-search">
        <a-list size="small" bordered style="border-bottom: none">
          <a-list-item>值班表</a-list-item>
        </a-list>
        <a-table
          ref="table"
          bordered
          :showHeader="false"
          :data-source="dataExcel"
          :columns="columns"
          :rowKey="(record) => record.file"
          :pagination="false"
        >
          <template slot="operation" slot-scope="text, record">
            <a @click="downloadRecord(record)">下载</a>
          </template>
        </a-table>
        <a-list
          size="small"
          bordered
          style="border-top: none; border-bottom: none"
        >
          <a-list-item>值班报告</a-list-item>
        </a-list>
        <a-table
          ref="table"
          bordered
          :showHeader="false"
          :data-source="dataWord"
          :columns="columns"
          :rowKey="(record) => record.file"
          :pagination="false"
        >
          <template slot="operation" slot-scope="text, record">
            <a @click="downloadRecord(record)">下载</a>
          </template>
        </a-table>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
//import { queryDutyDownLoadList, dutyDownload } from '@/api/modular/duty/index';

export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      formLoading: true,
      dataExcel: [],
      dataWord: [],
      dataPdf: [],
      columns: [
        { title: '标题', dataIndex: 'fileName', align: 'left' },
        { title: '操作', dataIndex: 'operation', align: 'center', width: 100, scopedSlots: { customRender: 'operation' } }
      ]
    }
  },
  methods: {
    downloadRecord(record) {
      dutyDownload({ file: record.file }).then(res => {
        let ele = document.createElement('a');
        let blob = new Blob([res]);
        ele.download = record.fileName;
        ele.href = URL.createObjectURL(blob);
        ele.click();
        URL.revokeObjectURL(ele.href);
      })
    },
    handleCancel() {
      this.visible = false;
      this.confirmLoading = false;
    },

    // 初始化方法
    download(record) {
      this.visible = true;
      this.formLoading = false;
      queryDutyDownLoadList({ dutyId: record.dutyId }).then(res => {
        this.dataWord = res.data.dutyReport;
        this.dataExcel = [res.data.duty];
        this.dataPdf = [res.data.dutyPdf];
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.ant-list {
  background: #f1f1f1;
}
</style>
